package timer

import (
	systime "time"

	"gitee.com/lossage/simplego/events"
)

type ITimer interface {
	Start(sender interface{})
	Stop() bool
	SetInterval(milSecond int64)
	GetElapsedEvent() *events.EventParams[interface{}]
	elapsed()
}

type timerBase struct {
	milSecond    int64
	sender       interface{}
	elapsedEvent *events.EventParams[interface{}]
}

func (_this *timerBase) setSender(sender interface{}) {
	_this.sender = sender
}

func (_this *timerBase) SetInterval(milSecond int64) {
	_this.milSecond = milSecond
}

func (_this *timerBase) GetElapsedEvent() *events.EventParams[interface{}] {
	if _this.elapsedEvent == nil {
		_this.elapsedEvent = events.NewEventParams[interface{}]()
	}
	return _this.elapsedEvent
}

func (_this *timerBase) riseEvent(tm systime.Time) {
	if _this.elapsedEvent != nil {
		_this.elapsedEvent.Event(_this.sender, tm)
	}
}
